// ==UserScript==
// @id             see2say player
// @name           see2say player
// @version        1.2
// @namespace
// @author         luomo
// @description
// @include        http://www.see2say.com/channel/album/*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// @run-at         window-load
// ==/UserScript==

var Mp3Url_Prefix = 'http://www.see2say.com/media.lib/app.x/streams/';
var Mp3ListUrl_Prefix = 'http://www.see2say.com/ajax/music/ajax_comm.aspx?classId=3&id=';
var Mp3Recomment_Prefix='http://www.see2say.com/ajax/music/ajax_album.aspx?classId=7&musicId=';
if (/album\/\?id=/i.test(location.href)) {
	var AlbumId = $("#txtId").val();
	AddConfig($('#p_music_num .blue_'));
	$('#p_music_num .blue_').removeClass('blue_').addClass('green_').click(function () {
		$.get(Mp3ListUrl_Prefix + AlbumId, function (data) {
			$('#p_music_num').removeClass().parent('div').attr("style", "margin-top:20px;");
			$('#lbl_album_music_list').html(data).show().removeClass();
			AddMp3Player();
			FixRecomment();
		});
		return false;
	});
} else if (/album\/music\.aspx\?id=/i.test(location.href)) {
	AddConfig($('#lbl_media_info'));
	$('#lbl_media_info').after('<a href="#addplayer" class="green_ addplayer">加载播放器</a>');
	$('.addplayer').click(function () {
		AddMp3Player();
		FixRecomment();
	});
}
function AddMp3Player() {
	var Mp3PlayerSite = 'http://see2say.ap01.aws.af.cm/';
	var UrlList_arr = new Array();
	var TitleList_arr = new Array();
	if (GM_getValue('loop', true))
		var loop = 1;
	else
		var loop = 0;
	$('.album_music_list').each(function (index) {
		var MusicDesc_arr = $(this).next('div').text().split('#||||||');
		var MusicUrl_arr = MusicDesc_arr[2].split("||||||");
		var Mp3Url_arr = MusicUrl_arr[0].split(".");
		var Mp3Url = Mp3Url_Prefix + Mp3Url_arr[0] + '-new.mp3';
		UrlList_arr.push(Mp3Url);
		TitleList_arr.push($.trim((index+1)+'.'+MusicDesc_arr[0] + ' ' + MusicDesc_arr[1]));
		$(this).after('<div class="album_music_list" style="height:30px"><embed src="' + Mp3PlayerSite + 'player.swf?url=' + Mp3Url + '&autoplay=0&loop=' + loop + '" type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" width="260" height="30"></div>');
	});
	var UrlList = UrlList_arr.join('||');
	var TitleList = TitleList_arr.join(' || ');
	$('.album_music_btn').after('<div class="album_music_list" style="height:40px"><embed src="' + Mp3PlayerSite + 'mainplayer_with_playlist.swf?url=' + UrlList + '&descri=' + TitleList + '&autoplay=0&loop=1" type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" width="350" height="40"></div>');
}
function AddConfig(item) {
	if (GM_getValue('loop', true))
		item.after('<span class="green_"><input type="checkbox" class="loop" checked />单曲循环</span>');
	else
		item.after('<span class="green_"><input type="checkbox" class="loop" />单曲循环</span>');
	$('.loop').change(function () {
		if ($(this).attr('checked') != undefined)
			GM_setValue('loop', true);
		else
			GM_setValue('loop', false);
	});
}
function FixRecomment() {
	$('.moper').each(function () {
		var RecommentImg=$(this).children('img:first');
		var MusicId=$(this).children('a:first').attr('href').split("|");
		RecommentImg.removeAttr('onclick').click(function () {
			$.get(Mp3Recomment_Prefix + MusicId[1], function (data) {
				if (data == "OK") {
					alert("推荐成功！");
					RecommentImg.attr('src','/images/default/music_tuijian1.jpg');
					RecommentImg.unbind('click');
				} else
					alert(text);
			});
		});
	});
}
